package com.ujiuye.controller;


import com.sun.media.jfxmedia.events.NewFrameEvent;
import com.ujiuye.config.*;
import com.ujiuye.pojo.Member;
import com.ujiuye.service.IMemberService;
import com.ujiuye.vo.MemberVo;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author zs
 * @since 2021-07-05
 */
@RestController
@RequestMapping("/member")
public class MemberController {
    @Reference
    private IMemberService memberService;



    @GetMapping("/membervo")
    public Result getMember(){
        try {
            MemberVo memberVo = memberService.memberList();
            return new Result(false,MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,memberVo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }

    @PostMapping("list")
    public PageResult list(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult pageResult = memberService.listPage(queryPageBean);
            return pageResult;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }


    @PostMapping("save")
    public Result save(@RequestBody Member member){
        try {
            memberService.save(member);
            return new Result(true, MessageConstant.ADD_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_MEMBER_FAIL);
        }
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") int id){
        try {
            Member member = memberService.getById(id);
            return new Result(true,MessageConstant.QUERY_MEMBER_SUCCESS,member);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MEMBER_FAIL);
        }


    }

    @PutMapping("update")
    public Result update(@RequestBody Member member){
        try {
            memberService.updateById(member);
            return new Result(true,MessageConstant.EDIT_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_MEMBER_FAIL);
        }

    }

    @DeleteMapping("remove/{id}")
    public  Result remove(@PathVariable("id") int id ){
        try {
            memberService.removeById(id);
            return new Result(true,MessageConstant.DELETE_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_MEMBER_FAIL);
        }
    }

    @RequestMapping("/download")
    public ResponseEntity<byte[]> download(int  currentPage, int  pageSize) throws IOException {
        List<Member> list = memberService.list(currentPage, pageSize);
        File file = memberToExcel(list);
        ResponseEntity<byte[]> download = DownLoad.download(file);
       return download;
    }
    @Value("${upload}")
    private String path;
    private File memberToExcel(List<Member> memberList){
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("会员信息");
        XSSFRow row = sheet.createRow(0);
        row.createCell(0).setCellValue("Id");
        row.createCell(1).setCellValue("用户名字");
        row.createCell(2).setCellValue("性别");
        row.createCell(3).setCellValue("出生日期");
        row.createCell(4).setCellValue("身份证号");
        row.createCell(5).setCellValue("手机号码");
        row.createCell(6).setCellValue("注册时间");
        row.createCell(7).setCellValue("邮箱地址");
        for (int i = 0; i < memberList.size(); i++) {
            Member member = memberList.get(i);
            XSSFRow rows = sheet.createRow(i+1);
            rows.createCell(0).setCellValue(member.getId());
            rows.createCell(1).setCellValue(member.getName());
            rows.createCell(2).setCellValue(member.getSex());
            LocalDate localDate = member.getBirthday();
            String birthday = LocalDateToString(localDate);
            rows.createCell(3).setCellValue(birthday);
            rows.createCell(4).setCellValue(member.getIdcard());
            rows.createCell(5).setCellValue(member.getPhonenumber());
            LocalDate localDate1 = member.getRegtime();
            String reg = LocalDateToString(localDate);
            rows.createCell(6).setCellValue(reg);
            rows.createCell(7).setCellValue(member.getEmail());
        }
        File file = new File(path+"会员信息.xls");
        try {
            OutputStream outputStream = new FileOutputStream(file);
            workbook.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }
    //用于localdate转换为string类型
    public String LocalDateToString(LocalDate localDate) {
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String dateStr = localDate.format(fmt);
        return dateStr;
    }


}

